<?php

namespace app\api\controller;

use app\common\controller\Api;

use app\admin\model\TaskJob;
use app\admin\model\Gateway;

use app\admin\model\Blacklist;

/**
 * Token接口
 */
class Fscallback extends Api
{

    protected $noNeedLogin = ['setTaskJobState', 'checkBlackList'];
    protected $noNeedRight = '*';

    protected $allowNet = array(
        '0' => '127.0.0.1',
        //'1' => '192.168.10.145'
    );

    public function _initialize()
    {
        parent::_initialize();
    }

    public function setTaskJobState() {
        
        if (! in_array($this->request->ip(), $this->allowNet)) {
            $this->error(__('You have no permission'));
        }
        $jobid = $this->request->request("jobid");
        if (empty($jobid)) {
            $this->error('[jobid]参数不能为空');
        }
        $job = TaskJob::get(['id' => $jobid]);
        if (empty($job)) {
            $this->error('编号为[' . $jobid . ']的任务JOB不存在');
        }
        $called = $this->request->request('called');
        if ($called == '1') {
            $job->called = 1;
            $job->calltime = time();
        }
        $callstate = $this->request->request('callstate');
        if (($callstate != NULL) && (in_array($callstate, array('none', 'success', 'fail')))) {
            $job->callstate = $callstate;
        }
        $failreason = $this->request->request('failreason');
        if ($failreason != NULL) {
            if ($failreason == '_undef_') {
                $job->failreason = '';
            } else {
                $job->failreason = $failreason;
                //$job->record_path = '';
            }
        }
        $record_path = $this->request->request('record_path');
        if ($record_path != NULL) {
            $job->record_path = $record_path;
        }
        $ivrstate = $this->request->request('ivrstate');
        if (($ivrstate != NULL) && (in_array($ivrstate, array('none', 'started', 'finished')))) {
            $job->ivrstate = $ivrstate;
        }
        $queuestate = $this->request->request('queuestate');
        if (($queuestate != NULL) && (in_array($queuestate, array('none', 'queued', 'recepted')))) {
            $job->queuestate = $queuestate;
        }
        $receptionextension = $this->request->request('receptionextension');
        if ($receptionextension != NULL) {
            if ($receptionextension == '_undef_') {
                $job->receptionextension = '';
            } else {
                $receptionextension = str_replace('@default', '', $receptionextension);
                $job->receptionextension = $receptionextension;
            }
        }
        $job->save();
        $gateway = Gateway::get(['name' => $job->callgateway]);
        if (!empty($gateway)) {
            $gateway->last_call_time = time();
            $gateway->save();
        }
        //$this->success('', ['job' => $job]);
        $this->success('更新成功');
    }

    public function checkBlackList() {
        if (! in_array($this->request->ip(), $this->allowNet)) {
            $this->error(__('You have no permission'));
        }
        $number = $this->request->request("number");
        if (empty($number)) {
            $this->error('[number]参数不能为空');
        }
        $blacklist = Blacklist::get(['number' => $number]);
        if (! empty($blacklist)) {
            die('1');
        } else {
            die('0');
        }
    }

    /**
     * 检测Token是否过期
     *
     */
    /*
    public function check()
    {
        $token = $this->auth->getToken();
        $tokenInfo = \app\common\library\Token::get($token);
        $this->success('', ['token' => $tokenInfo['token'], 'expires_in' => $tokenInfo['expires_in']]);
    }
    */

    /**
     * 刷新Token
     *
     */
    /*
    public function refresh()
    {
        $token = $this->auth->getToken();
        $tokenInfo = \app\common\library\Token::get($token);
        $tokenInfo->expiretime = time() + 2592000;
        $tokenInfo->save();
        $this->success('', ['token' => $tokenInfo['token'], 'expires_in' => $tokenInfo['expires_in']]);
    }
    */

}
